package com.tanguy.user.ui.me.activity

import android.app.Activity
import android.content.Intent
import cn.sinata.xldutils_kotlin.activity.CropImageActivity
import cn.sinata.xldutils_kotlin.activity.SelectPhotoDialog
import com.tanguy.user.App.Companion.app
import com.tanguy.user.Const
import com.tanguy.user.R
import com.tanguy.user.base.ImmersionTitleActivity
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.ui.window.OnePickerViewPopupWindow
import com.tanguy.user.ui.window.UpMassageWindow
import com.tanguy.user.utlis.extend.*
import kotlinx.android.synthetic.main.activity_me_message.*
import org.jetbrains.anko.startActivityForResult

/**
 * Created by dab on 2017/10/9 0009 17:27
 * 个人信息
 */
class MeMessageActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_me_message
    override fun initView() {
        super.initView()
        title = "个人信息"
        tv_name.text = app.user.name
        tv_signature.text = app.user.autograph
        iv_portrait.setImageURI(app.user.headImg)
        ttv_sex.rightText = app.user.sexString

    }

    override fun initEvent() {
        super.initEvent()
        click(R.id.tv_name) {
            showPopupWindow(UpMassageWindow(this, 1, app.user.name) {
                setText(R.id.tv_name, it)
            })
        }
        click(R.id.tv_signature) {
            showPopupWindow(UpMassageWindow(this, 2, app.user.autograph) {
                setText(R.id.tv_signature, it)
            })
        }

        click(ttv_sex) {
            hideOrShowKeyboard(true)
            showPopupWindow(OnePickerViewPopupWindow(this@MeMessageActivity, arrayListOf("男", "女")) { _, msg ->
                ttv_sex.rightText = msg
                if (msg != app.user.sexString) {
                    updateInfo(sex = app.user.getSexByString(msg)) {
                        app.user.setSex(msg)
                    }
                }
            })
        }
        click(iv_portrait) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.IMAGE_PORTRAIT)
        }
        click(tv_name_clear) { tv_name.text = "" }
        click(tv_signature_clear) { tv_signature.text = "" }

    }

    /**
     * 修改个人信息
     */
    fun updateInfo(headImg: String = "", name: String = "", autograph: String = "", sex: Int? = null, succeed: () -> Unit) {
        HttpManager.updateInfo(app.user.userId, headImg, name, autograph, sex)
                .requestSucceed(this) {
                    succeed.invoke()
                }

    }

    override fun onBackPressed() {
        setResult(Activity.RESULT_OK)
        finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == Const.RequestCode.IMAGE_PORTRAIT && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                startActivityForResult<CropImageActivity>(Const.RequestCode.IMAGE_CROP_PORTRAIT, Pair("uri", path!!), Pair("mode", 1))

            }
            if (requestCode == Const.RequestCode.IMAGE_CROP_PORTRAIT && data != null) {
                val path = data.getStringExtra("path")
                iv_portrait.setImageByFile(path)
                HttpManager.uploadFile(this, path)
                        .request(this) {
                            if (it == null) return@request
                            updateInfo(headImg = it) {
                                app.user.headImg = it
                            }
                        }
            }
        }
    }
}